/*
 * Created on Aug 24, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ba360.struts.action;

import java.sql.Connection;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ba360.portal.common.PortalConstants;
import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.portal.database.dbo.User;
import com.ba360.portal.util.Log;

/**
 * @author ROnat
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class UserActions extends Action {
	private static final int ACTION_SAVETHISAD = 1;
	private static final int ACTION_EMAILTOBUDDY = 2;
	private static final int ACTION_SAVESEARCHES = 3;
	private static final int ACTION_RESPONDTOAD = 4;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
	 HttpSession session = request.getSession();
	 int action = Integer.parseInt(request.getParameter(PortalConstants.ACTION));
	 if (action==ACTION_SAVETHISAD)
	 {
	 	int adId = Integer.parseInt(request.getParameter("advId"));
	 	User user = (User)session.getAttribute(PortalConstants.USER);
	 	double userId = user.getUserId();
	 	saveThisAd(adId,userId);
	 	mapping.findForward(PortalConstants.PAGE_SUCCESS);
	 }

	 return null;
	}
	
	public void saveThisAd(int adId, double userId) throws Exception
	{
		 DBConnectionImpl dbConn = new DBConnectionImpl();
		 Connection conn = dbConn.getConnection();
		 Statement stmt = conn.createStatement();
		 String statement ="insert into favoriteads(fav_adid,userid) values("+adId+","+userId+")";
		 Log.init(this.getClass());
		 Log.info(statement);
		 stmt.execute(statement);
		 stmt.close();
		 conn.close();
	}
}
